package email

import (
	"context"
	"fmt"
    "crypto/tls"
	"github.com/cloudwego/hertz/pkg/common/hlog"
	"github.com/jordan-wright/email"
	"mall_frontend_api/biz/config"
	"net/smtp"
)

func SendMail(ctx context.Context, to string, subject, body string) error {
	e := email.NewEmail()
    e.From = fmt.Sprintf("%s <%s>", config.Values.Email.From, config.Values.Email.User)
	e.To = []string{to}
	e.Subject = subject
	e.HTML = []byte(body)
	err := e.SendWithTLS(fmt.Sprintf("%s:%s", config.Values.Email.Host, config.Values.Email.Port),
		smtp.PlainAuth("",
			config.Values.Email.User,
			config.Values.Email.Identity,
			config.Values.Email.Host), &tls.Config{InsecureSkipVerify: true})
	if err != nil {
		hlog.CtxErrorf(ctx, "smtp error: %v", err)
		return err
	}
	return nil
}
